﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VitalyZayko.Code4Blog
{
    public partial class OptionPageControl : UserControl
    {
        internal Code4BlogOptions OptionPage;

        private Color[] webColors = new Color[] { Color.White, Color.Aqua, Color.Black, Color.Blue, Color.Fuchsia, Color.Green, Color.Gray, 
            Color.Lime, Color.Maroon, Color.Navy, Color.Olive, Color.Purple, Color.Red, Color.Silver, Color.Teal, Color.Yellow };


        public OptionPageControl()
        {
            InitializeComponent();
        }

        public void Initialize()
        {
            foreach (System.Drawing.FontFamily family in System.Drawing.FontFamily.Families)
                comboBoxFont.Items.Add(family.Name);

            numericUpDownWidth.Value = OptionPage.MaxWidth;
            numericUpDownHeight.Value = OptionPage.MaxHeight;

            comboBoxOdd.Text = OptionPage.OddColor;
            comboBoxEven.Text = OptionPage.EvenColor;

            comboBoxFont.Text = OptionPage.FontFamily;
            try
            {
                numericUpDownFontSize.Value = OptionPage.FontSize;
            }
            catch
            {
                numericUpDownFontSize.Value = 8;
            }

            checkBoxBorder.Checked = OptionPage.DrawBorder;

            checkBoxNumbers.Checked = OptionPage.LineNumbers;
        }

        private void numericUpDownWidth_ValueChanged(object sender, EventArgs e)
        {
            OptionPage.MaxWidth = numericUpDownWidth.Value;
        }

        private void numericUpDownHeight_ValueChanged(object sender, EventArgs e)
        {
            OptionPage.MaxHeight = numericUpDownHeight.Value;
        }

        private void comboBoxOdd_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0)
                return;
            
            Color bkColor = webColors[e.Index];
            Rectangle colorRec = new Rectangle(e.Bounds.Left + 1, e.Bounds.Top + 1, 25, e.Bounds.Height - 2);
            e.Graphics.FillRectangle(new SolidBrush(bkColor), colorRec);
            

            Color textColor = Color.FromArgb(bkColor.ToArgb() ^ 0xffffff);
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Near;
            Rectangle textRec = new Rectangle(30, e.Bounds.Top, e.Bounds.Width - 30, e.Bounds.Height);
            e.Graphics.DrawString((sender as ComboBox).Items[e.Index].ToString(), (sender as ComboBox).Font, new SolidBrush(Color.Black), textRec, sf);
        }

        private void comboBoxOdd_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((sender as ComboBox).Name == "comboBoxOdd")
            {
                OptionPage.OddColor = Color.FromName(comboBoxOdd.Text).Name;
                
            }
            else
            {
                OptionPage.EvenColor = Color.FromName(comboBoxEven.Text).Name;
            }
            

        }

        private void comboBoxFont_SelectedIndexChanged(object sender, EventArgs e)
        {
            OptionPage.FontFamily = comboBoxFont.Text;
        }

        private void numericUpDownFontSize_ValueChanged(object sender, EventArgs e)
        {
            OptionPage.FontSize = numericUpDownFontSize.Value;
        }

        private void checkBoxBorder_CheckedChanged(object sender, EventArgs e)
        {
            OptionPage.DrawBorder = checkBoxBorder.Checked;
        }

        private void checkBoxNumbers_CheckedChanged(object sender, EventArgs e)
        {
            OptionPage.LineNumbers = checkBoxNumbers.Checked;
        }

        
    }
}
